/**
* <p> title ： ImageStatusLineContributionItem.java</p>
* <p> package ： com.akeil.sulfur.statusline</p>
* <p> description ：TODO</p>
* <p> copyright ： 哈尔滨工业大学(C) 2019</p>
* <p> company ： 哈尔滨工业大学AS&MT</p>
* <p> author ： 金雪松 jinxuesong@163.com</p>
* <p> date ： 2019年11月1日 下午3:53:20</p>
* <p> version ： v1.0 </p>
*
* Modification  History:
* Date         Author        Version        Discription
* -----------------------------------------------------------------------------------
* 2019年11月1日       金雪松          1.0             1.0
* Why & What is modified: <修改原因描述>
*/
package com.akeil.flash.statusline;

import java.util.Timer;
import java.util.TimerTask;

import org.eclipse.jface.action.StatusLineContributionItem;
import org.eclipse.jface.action.StatusLineLayoutData;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

/**
 * <p> classname ： ImageStatusLineContributionItem</p>
 * <p> description ：TODO</p>
 * <p> author ： 金雪松 jinxuesong@163.com</p>
 * <p> date ： 2019年11月1日 下午3:53:20</p>
 */
public class GreenTextStatusLineContributionItem extends StatusLineContributionItem {
	private CLabel label;
	private int widthHint;
	private int heightHint;
	private String text = "测试";
	private int status,current_status;
	public  final Color color_READY = new Color(null,0,255,0);
	public final Color color_START = new Color(null,255,0,0);
	
	public Color color_FINISH = new Color(null,32,32,32);
	public Color color_WHITE = new Color(null,32,32,32);
	
	private Color current_color,current_color_show;
	private Timer timer;
	private boolean flick = false;
	
	public GreenTextStatusLineContributionItem(String id) {
		super(id);
		// TODO 自动生成的构造函数存根
	}

	public GreenTextStatusLineContributionItem(String id, int charWidth) {
		super(id, charWidth);
		// TODO 自动生成的构造函数存根
		this.widthHint = charWidth;
		timer = new Timer();
		//startTimer();
	}

	@Override
	public void fill(Composite parent) {
		// TODO 自动生成的方法存根
		Label separator = new Label(parent, SWT.SEPARATOR);
		label = new CLabel(parent, SWT.SHADOW_NONE);

		GC gc = new GC(parent);
		gc.setFont(parent.getFont());
		FontMetrics fm = gc.getFontMetrics();
		Point extent = gc.textExtent(text);
		heightHint = fm.getHeight();
		gc.dispose();

		StatusLineLayoutData statusLineLayoutData = new StatusLineLayoutData();
		statusLineLayoutData.widthHint = widthHint;
		statusLineLayoutData.heightHint = heightHint;
		label.setLayoutData(statusLineLayoutData);
		label.setText(text);
		color_FINISH = label.getBackground();
		color_WHITE = label.getBackground();
		current_color = color_FINISH;
		Font songFont = new Font(null, "黑体", 16, SWT.NONE);
		label.setFont(songFont);
		
		current_status = 0;
	}

	public int getStatus() {
		return status;
	}

	public void setText(String text) {
		this.text = text;
		if(label != null){
			if(text.equals("试验结束")){
				status = 0;
				if(status != current_status){
					current_status = status;
					//label.setBackground(color_FINISH);
					current_color = color_FINISH;
				}
			}else if(text.equals("准备试验")){
				status = 1;
				if(status != current_status){
					current_status = status;
					//label.setBackground(color_READY);
					current_color = color_READY;
				}
			}else if(text.equals("正在试验")){
				status = 2;
				if(status != current_status){
					current_status = status;
					//label.setBackground(color_START);
					current_color = color_START;
				}
			}else{
				
			}
		}
	}
	
	public void startTimer(){
		timer.purge();
		timer.schedule(new TimerTask(){
	
			@Override
			public void run() {
				
			}
		},1000,1000);
	}
	
	public void stopTimer(){
		timer.cancel();
		timer.purge();
	}
	
	public void showStatus(){
		if(flick){
			if(label != null && !(label.isDisposed())){
				label.getDisplay().asyncExec(new Runnable(){

					@Override
					public void run() {
						// TODO 自动生成的方法存根
						if(status == 0){
							label.setBackground(color_FINISH);
						}else if(status == 1){
							label.setBackground(color_READY);
						}else if(status == 2){
							label.setBackground(color_START);
						}
						
						label.setText(text);
					}
				});
			}
		}else{
			if(label != null && !(label.isDisposed())){
				label.getDisplay().asyncExec(new Runnable(){

					@Override
					public void run() {
						// TODO 自动生成的方法存根
						label.setBackground(color_WHITE);
						label.setText(text);
					}
				});
				
			}
		}
		flick = !flick;
	}
}